package org.sol.language.parser.internal;

import java.io.*;
import java.util.Scanner;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.sol.language.parser.grammar.SolLexer;
import org.sol.language.parser.grammar.SolParser;

public class SolRepl {

	public static void main(String[] args) throws IOException, RecognitionException {
        if (args.length == 0) {
            new SolRepl().processInteractive();
        } else if (args.length == 1) { 
        	// name of file to process was passed in
            new SolRepl().processFile(args[0]);
        } else { 
        	// more than one command-line argument
            System.err.println("usage: java " + SolRepl.class.getCanonicalName() + " [file-name]");
        }
    }

    private void processFile(String filePath) throws IOException, RecognitionException {
        CommonTree ast = getAST(new FileReader(filePath));
        //System.out.println(ast.toStringTree()); // for debugging
        processAST(ast);
    }

    private CommonTree getAST(Reader reader) throws IOException, RecognitionException {
        SolParser tokenParser = new SolParser(getTokenStream(reader));
        SolParser.module_return parserResult =
            tokenParser.module(); // start rule method
        reader.close();
        return (CommonTree) parserResult.getTree();
    }

    private CommonTokenStream getTokenStream(Reader reader) throws IOException {
        SolLexer lexer = new SolLexer(new ANTLRReaderStream(reader));
        return new CommonTokenStream(lexer);
    }

    private void processAST(CommonTree ast) throws RecognitionException {
//        SolTree treeParser = new SolTree(new CommonTreeNodeStream(ast));
//        treeParser.script(); // start rule method
    }

    private void processInteractive() throws IOException, RecognitionException {
//        SolTree treeParser = new SolTree(null); // a TreeNodeStream will be assigned later
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.print("math> ");
            String line = scanner.nextLine().trim();
            if ("quit".equals(line) || "exit".equals(line)) break;
 //           processLine(treeParser, line);
        }
    }

    // Note that we can't create a new instance of SolTree for each
    // line processed because it maintains the variable and function Maps.
//    private void processLine(SolTree treeParser, String line) throws RecognitionException {
//        // Run the lexer and token parser on the line.
//        SolLexer lexer = new SolLexer(new ANTLRStringStream(line));
//        SolParser tokenParser = new SolParser(new CommonTokenStream(lexer));
//        SolParser.statement_return parserResult =
//            tokenParser.statement(); // start rule method
//
//        // Use the token parser to retrieve the AST.
//        CommonTree ast = (CommonTree) parserResult.getTree();
//        if (ast == null) return; // line is empty
//
//        // Use the tree parser to process the AST.
//        treeParser.setTreeNodeStream(new CommonTreeNodeStream(ast));
//        treeParser.statement(); // start rule method
//    }
}